package com.cos.emp.web.controller;

import com.cos.emp.common.Const;
import com.cos.emp.entity.Emp;
import com.cos.emp.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * @author: COS
 * @time: 2022/9/29 11:26
 * @description:
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpServices es;

    @RequestMapping("/getAll")
    public String getAll(Model model) {
        model.addAttribute("el", es.getAll());
        return "index";
    }


    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public String doLogin(String username, String password, HttpSession session) {
        //访问业务层
        Emp emp = es.doLogin(username, password);
        // 登录保存信息
        if (emp != null) {
            session.setAttribute(Const.IS_LOGIN, emp);
            return "redirect:/emp/getAll";
        } else {
            return "redirect:/login.jsp";
        }
    }

}
